/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.content;

import com.jb.web.application.SystemUtil;
import com.jb.web.bean.Region;
import com.jb.web.bean.TextArea;
import com.jb.web.bean.TextField;
import com.jb.web.content.ContentManager;
import com.jb.web.context.WebContext;


//portal provider
public class ContentEditor implements PortalContentProvider {
    boolean personalizable;

    public ContentEditor(boolean personalizable) {
        this.personalizable = personalizable;
    }

    public StringBuffer getContent(WebContext ctx, Portal portal) {
        return new StringBuffer("" + ContentManager.getInstance(ctx, personalizable).get(portal.getClientId()));
    }

    public void delete(WebContext ctx, Portal portal) {
        ContentManager.getInstance(ctx, personalizable).delete(portal.getClientId());
    }

    public void edit(WebContext ctx, Portal portal) {
        String content = (String) ContentManager.getInstance(ctx, personalizable).get(portal.getClientId());
        Region editorPanel = (Region) SystemUtil.showDialogBox(ctx, "textEditor", "editorPanel");

        if (editorPanel == null) {
            ctx.addErrorMessage(this, "Failed to find editor definition textEditor.editorPanel");

            return;
        }

        TextArea ta = (TextArea) editorPanel.getDescendantByClientId("textBody");
        TextField tf = (TextField) editorPanel.getDescendantByClientId("contentName");

        if ((tf == null) || (ta == null)) {
            ctx.addErrorMessage(this, "textEditor.editorPanel must have contentName and textBody fields");

            return;
        }

        tf.setValue(portal.getClientId());
        ta.setValue(content);
    }
}
